/*
 * Tools.h
 *
 *  Created on: 26 févr. 2013
 *      Author: raph
 */

#ifndef TOOLS_H_
#define TOOLS_H_

#include <iostream>
#include <stdexcept>

class TabTools
    {
    public:
        template<typename T> static void fillTab(T *tab, unsigned int size);
        template<typename T> static void printTab(T *tab, unsigned int l, unsigned int r);
        template<typename T> static void deleteVector(T **tab);
//	template<typename T> static void deleteMatrix(T ***tab, unsigned int size);
        template<typename T> static T* addSequential(T *tab1, T *tab2, unsigned int size);

    };

template<typename T> void TabTools::fillTab(T *tab, unsigned int size)
    {
    const unsigned int n = size;
    for (unsigned int i = 0; i < n; ++i)
        {
        tab[i] = i;
        }
    }

template<typename T> void TabTools::printTab(T *tab, unsigned int l, unsigned int r)
    {
    std::cout << "[";
    const unsigned int n = r;
    for (unsigned int i = l; i < n; ++i)
        {

        std::cout << tab[i] << ",";
        }
    std::cout << "]";
    }

template<typename T> void TabTools::deleteVector(T **tab)
    {
    try
        {
        delete[] *tab;
        *tab = nullptr;
        }
    catch (...)
        {
        throw;
        }
    }

//template<typename T> void TabTools::deleteMatrix(T ***tab, unsigned int size)
//    {
//    const unsigned int n = size;
//    for (unsigned int i = 0; i < n; ++i)
//	{
//	TabTools::deleteVector(*tab[i]);
//	}
//    delete[] **tab;
//    **tab = 0;
//    }

template<typename T> T* TabTools::addSequential(T *tab1, T *tab2, unsigned int size)
    {
    try
        {
        if (tab1 == 0 || tab2 == 0)
            {
            throw std::bad_alloc();
            }
        T* tabResult = new T[size];
        const unsigned int n = size;

        for (unsigned int i = 0; i < n; ++i)
            {
            tabResult[i] = tab1[i] + tab2[i];
            }

        return tabResult;
        }
    catch (std::bad_alloc&)
        {
        throw;
        }
    catch (std::exception&)
        {
        throw;
        }
    catch (...)
        {
        throw;
        }
    }

#endif /* TOOLS_H_ */
